package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Category;
import com.itheima.entity.Employee;
import com.itheima.service.CategoryService;
import com.itheima.service.impl.CategoryServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author fengzhu
 * @since 2024-10-10
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 实现查询所有的菜品
     * @param page 页码
     * @param pageSize 页数
     * @param name 拿的名字，c
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        log.info("page={},pageSize={},name={}",page,pageSize,name);
        //构造分页,作为条件
        Page<Category> pageInfo =new Page<>(page,pageSize);
        //拿菜的名字

        categoryService.page(pageInfo);
        return R.success(pageInfo);
    }

    /**
     * 实现两个新增功能
     * @param request 请求
     * @param category 反序列化为JAVA对象
     * @return R.success
     */
    @PostMapping
    public R<String> addCategory(HttpServletRequest request, @RequestBody Category category){
       Long categoryID = (Long)request.getSession().getAttribute("employee");

       category.setUpdateUser(categoryID);
       categoryService.save(category);

        return R.success("成功");
    }

    /**
     * 实现修改功能
     * @param request 请求
     * @param category java对象
     * @return R.success
     */
    @PutMapping
    public R<String> updateCategory(HttpServletRequest request, @RequestBody Category category){
        Long categoryID = (Long)request.getSession().getAttribute("employee");
        category.setUpdateUser(categoryID);
        categoryService.updateById(category);
        return R.success("成功");
    }

    /**
     * 实现分类管理的删除
     * @param ids 前端的请求头中只有id
     * @return
     */
    @DeleteMapping
    public R<String> deleteCategory(@RequestParam Long ids){
        categoryService.removeById( ids);
        return R.success("成功 ");
    }

    /**
     *实现展示商品的类别，并且通过数据库表可以知道type为1表示餐品，2是套餐
     * @param type 前端传来的参数
     * @return List<Category>
     */
    private List<Category> getCategories(Integer type) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        if (type != null) {
            queryWrapper.eq(Category::getType, type);
        }
        return categoryService.list(queryWrapper);
    }

    @GetMapping("/list")
    public R<List<Category>> getCategoryByType(@RequestParam(required = false) Integer type) {
        List<Category> categoryList = getCategories(type);
        return R.success(categoryList);
    }
}
